package com.kang.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.alibaba.fastjson.JSON;
import com.kang.pojo.AjaxResult;
import com.kang.service.AssistService;
import com.kang.service.AuditService;
import com.vonechina.entity.page.AuditPurchaseOrderPage;
import com.vonechina.entity.page.AuditPurchaseRequestPage;
import com.vonechina.entity.page.AuditReturnRequestPage;

/**
 * 审批控制层
 * 
 * @author zs614
 *
 */
@Controller
@RequestMapping("audit")
@SessionAttributes({ "auditPurchaseRequestPage" })
public class AuditController extends com.kang.controller.Controller {
	/**
	 * 审批服务层
	 */
	@Resource
	private AuditService auditService;
	/**
	 * 通用服务层
	 */
	@Resource
	private AssistService assistService;

	/**
	 * 采购申请列表
	 * 
	 * @param session
	 * @param model
	 * @param auditPurchaseRequestPage
	 *            采购申请搜索条件自动封装
	 * @return 返回jsp页面
	 */
	@RequestMapping("listPurchaseRequestAudit")
	public String listPurchaseRequestAudit(HttpSession session, Model model,
			AuditPurchaseRequestPage auditPurchaseRequestPage) {
		initRight(session);
		auditPurchaseRequestPage.setUserID(userID);
		auditPurchaseRequestPage.setAuditType(1);
		List<Map<String, Object>> list = auditService.listAudit(auditPurchaseRequestPage);
		// 查询流程级别list
		model.addAttribute("list", list);
		// 分页信息
		auditPurchaseRequestPage.setPageUrl("audit/listPurchaseRequestAudit.do");
		model.addAttribute("page", auditPurchaseRequestPage);
		return "audit/purchaseRequestAudit";
	}

	/**
	 * 
	 * @param session
	 * @param model
	 * @param auditPurchaseOrderPage
	 *            采购订单搜索条件自动封装
	 * @return
	 */
	@RequestMapping("listPurchaseOrderAudit")
	public String listPurchaseOrderAudit(HttpSession session, Model model,
			AuditPurchaseOrderPage auditPurchaseOrderPage) {
		initRight(session);
		auditPurchaseOrderPage.setUserID(userID);
		auditPurchaseOrderPage.setAuditType(2);
		List<Map<String, Object>> list = auditService.listAudit(auditPurchaseOrderPage);
		// 查询流程级别list
		model.addAttribute("list", list);
		// 分页信息
		auditPurchaseOrderPage.setPageUrl("audit/listPurchaseOrderAudit.do");
		model.addAttribute("page", auditPurchaseOrderPage);
		return "audit/purchaseOrdertAudit";
	}
	/**
	 * 
	 * @param session
	 * @param model
	 * @param auditReturnRequestPage
	 *            退货申请搜索条件自动封装
	 * @return
	 */
	@RequestMapping("listReturnRequestAudit")
	public String listReturnRequestAudit(HttpSession session, Model model,
			AuditReturnRequestPage auditReturnRequestPage) {
		initRight(session);
		auditReturnRequestPage.setUserID(userID);
		auditReturnRequestPage.setAuditType(3);
		List<Map<String, Object>> list = auditService.listAudit(auditReturnRequestPage);
		// 查询流程级别list
		model.addAttribute("list", list);
		// 分页信息
		auditReturnRequestPage.setPageUrl("audit/listReturnRequestAudit.do");
		model.addAttribute("page", auditReturnRequestPage);
		return "audit/returnRequestAudit";
	}

	/**
	 * 通过审批
	 * 
	 * @param session
	 * @param id
	 *            审核记录ID
	 * @return
	 */
	@RequestMapping("updatepassapproveRequestAudit")
	@ResponseBody
	public String updatePassAudit(HttpSession session, String id) {
		AjaxResult result = new AjaxResult();
		initRight(session);
		int i = auditService.passapproveRequest(Integer.parseInt(id), userID);
		if (i > 0) {
			result.setResult("success");
		} else {
			result.setResult("fail");
		}
		return JSON.toJSONString(result);
	}

	/**
	 * 拒绝审批
	 * 
	 * @param session
	 * @param id
	 *            审核记录ID
	 * @return
	 */
	@RequestMapping("updaterejectapproveRequestAudit")
	@ResponseBody
	public String updateRejectAudit(HttpSession session, String id) {
		AjaxResult result = new AjaxResult();
		initRight(session);
		int i = auditService.rejectapproveRequest(Integer.parseInt(id), userID);
		if (i > 0) {
			result.setResult("success");
		} else {
			result.setResult("fail");
		}
		return JSON.toJSONString(result);
	}
	
	
}